package net.homeip.mmhg.web;

import net.homeip.mmhg.domain.Team;
import net.homeip.mmhg.events.EventPublisher;
import net.homeip.mmhg.events.PositionEvent;
import net.homeip.mmhg.events.Event.EventType;
import net.homeip.mmhg.game.AttemptResultInfo;
import net.homeip.mmhg.game.GameContext;
import net.homeip.mmhg.game.AttemptResultInfo.AttemptResult;

import net.homeip.mmhg.security.SecurityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Shows the page with current task.
 *
 * @author ilya gerasimov
 */
@Controller
public class TaskAccessController {
	
	private int EXTRA_INTREVAL = 7;
	private static String MESSAGE_ATTR = "message";
	
	private GameContext context;
	private SecurityManager securityManager;
	private EventPublisher publisher;
	
	@Autowired
	public void setGameService(GameContext context) {
		this.context = context;
	}
	
	@Autowired
	public void setSecurityManager(SecurityManager securityManager) {
		this.securityManager = securityManager;
	}
	
	@Autowired
	public void setEventPublisher(EventPublisher publisher) {
		this.publisher = publisher;
	}
	
	@RequestMapping(value = "/home.htm")
	public ModelAndView getTaskPage() {
		Team team = securityManager.getCurrentUser();
		ModelAndView mav = new ModelAndView("home");
		mav.addObject("position", context.getTeamPosition(team.getId()));
		mav.addObject("interval", context.getSecUntilNextEvent(team) + EXTRA_INTREVAL);
		return mav;
		
	}
	
	@ModelAttribute("gameContext")
	public GameContext getGameContext() {
		return context;
	}
	
	@RequestMapping("/try_guess.htm")
	public ModelAndView tryGuess(@RequestParam(value = "guess", required = false) String guess) {
		ModelAndView mav = new ModelAndView("forward:/home.htm");
		if ( (guess != null) && (!guess.isEmpty())){
			AttemptResultInfo resultInfo = context.tryGuess(securityManager.getCurrentUser().getId(), guess);
			if (resultInfo.getResult() == AttemptResult.FINISHED) {
				mav.addObject(MESSAGE_ATTR, "messages.task.finished");
			} else if (resultInfo.getResult() == AttemptResult.MISSED) {
				mav.addObject(MESSAGE_ATTR, "messages.task.missed");
			} else if (resultInfo.getResult() == AttemptResult.PARTIALLY_GUESSED) {
				mav.addObject(MESSAGE_ATTR, "messages.task.partially_guessed");
				mav.addObject("infoString", resultInfo.getWrongAnswers());
			} else if(resultInfo.getResult() == AttemptResult.GUESSED) {
				mav.addObject(MESSAGE_ATTR, "messages.task.guessed");
			} else {
				mav.addObject(MESSAGE_ATTR, "messages.task.rejected");
			}
		}
		return mav;
	}
	
	@RequestMapping("/reject_task.htm")
	public ModelAndView reject() {
		Team team = securityManager.getCurrentUser();
		context.rejectTask(team);
		publisher.publish(new PositionEvent(EventType.TASK_REJECTED,
										context.getTeamPosition(team.getId())));
		return new ModelAndView("forward:/home.htm", 
							MESSAGE_ATTR, "messages.task.rejected_by_himself");
	}
}
